<?php
namespace Admin\Controller;

class LabelController extends BaseController
{

    public function index()
    {

        $label = M('store_products_label')->where(['local_id' => session('userInfo')['local_id']])->select();

        $this->assign(['label' => $label]);
        $this->display();
    }

    public function add()
    {

        if ($_POST) {
            $name = I('name', '', htmlspecialchars());
            $label = M('store_products_label');
            $data = [
                'label_name' => $name,
                'local_id' => session('userInfo')['local_id']
            ];
            $label->add($data);
            $this->success('成功', '/admin/label/index');exit;
        }
        $this->display();
    }

    public function edit(){
        $id = I('id', '', htmlspecialchars());
        $label = M('store_products_label');
        $where = [
            'local_id' => session('userInfo')['local_id'],
            'label_id' => $id
        ];
        if ($_POST) {
            $name = I('name', '', htmlspecialchars());
            $data = [
                'label_name' => $name,
            ];
            $label->where($where)->save($data);
            $this->success('成功', '/admin/label/index');exit;
        }
        $labelInfo = $label->where($where)->find();
        $this->assign(['labelInfo' => $labelInfo]);
        $this->display();
    }

    public function delete(){
        $id = I('id', '', htmlspecialchars());
        $label = M('store_products_label');
        $where = [
            'local_id' => session('userInfo')['local_id'],
            'label_id' => $id
        ];
        $label->where($where)->delete();
        $this->success('完成', '/admin/label/index');exit;
    }

}